package com.mozai.shopping.controller;

import com.mozai.shopping.model.Activity;
import com.mozai.shopping.model.base.BaseResponse;
import com.mozai.shopping.model.base.ErrorMessage;
import com.mozai.shopping.model.http.ActivityAddReq;
import com.mozai.shopping.model.http.ActivityDelReq;
import com.mozai.shopping.model.http.ActivityPageReq;
import com.mozai.shopping.model.http.ActivityUpdateReq;
import com.mozai.shopping.model.vo.PageResp;
import com.mozai.shopping.service.ActivityService;
import com.mozai.shopping.utils.bean.ComonQueryBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
@Slf4j
@RestController
@RequestMapping(value = "/activity")
public class ActivityController {

    @Autowired
    ActivityService activityService;

    @RequestMapping(value = "/addActivity",method = RequestMethod.POST)
    public BaseResponse addActivity(@Valid @RequestBody ActivityAddReq activityAddReq){
        log.info("活动新增：{}"+activityAddReq);
        try{
            Activity activity =new Activity();
            BeanUtils.copyProperties(activityAddReq,activity);
            activityService.insertActivity(activity);
        }catch (Exception e){
            log.error("活动新增异常：{}",e);
        }
        return BaseResponse.getOK();
    }


    @RequestMapping(value = "/delActivity",method = RequestMethod.POST)
    public BaseResponse delActivity(@Valid @RequestBody ActivityDelReq activityDelReq){
        log.info("活动删除：{}"+activityDelReq);
        try{
            Activity activity =new Activity();
            BeanUtils.copyProperties(activityDelReq,activity);
            if (activityService.delActivity(activity)==0) {
                return BaseResponse.error(ErrorMessage.ACTIVITY_DEL);
            }
        }catch (Exception e){
            log.error("活动删除异常：{}",e);
        }
        return BaseResponse.getOK();
    }


    @RequestMapping(value = "/modifyActivity",method = RequestMethod.POST)
    public BaseResponse modifyActivity(@Valid @RequestBody ActivityUpdateReq activityUpdateReq){
        log.info("活动更新：{}"+activityUpdateReq);
        try{
            Activity activity =new Activity();
            BeanUtils.copyProperties(activityUpdateReq,activity);
            if (activityService.modifyActivity(activity)==0) {
                return BaseResponse.error(ErrorMessage.ACTIVITY_DEL);
            }

        }catch (Exception e){
            log.error("活动更新异常：{}",e);
        }
        return BaseResponse.getOK();
    }


    @RequestMapping(value = "/queryActivityList",method = RequestMethod.POST)
    public BaseResponse queryActivity(@Valid @RequestBody ActivityPageReq activityPageReq){
        log.info("活动列表查询：{}",activityPageReq);
        PageResp<Activity> activityPageResp =new PageResp<>();
        try{
            ComonQueryBean commonQueryBean=new ComonQueryBean();
            Activity activity =new Activity();
            BeanUtils.copyProperties(activityPageReq,activity);
            BeanUtils.copyProperties(activityPageReq,commonQueryBean);
            activityPageResp = activityService.queryActivity(activity,commonQueryBean);

        }catch (Exception e){
            log.error("活动更新异常：{}",e);
        }
        return BaseResponse.ok(activityPageResp);
    }
}
